﻿using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Threading;
using System.Windows;
using WPFExcelReport.Helpers;
using System.Windows.Controls;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for EditEventWindow.xaml
    /// </summary>
    public partial class TAB50RV
    {
        #region ==dwMUD===========
        public static readonly DependencyProperty dwMUDProperty = DependencyProperty.Register("dwMUD",
            typeof(DataRow), typeof(TAB50RV),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwMUDChanged));
        public DataRow dwMUD
        {
            get
            {
                return (DataRow)GetValue(dwMUDProperty);
            }
            set
            {
                SetValue(dwMUDProperty, value);
            }
        }
        private static void OndwMUDChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TAB50RV;
            if (src != null)
            {
                src.dwMUD = (DataRow)e.NewValue;
            }
        }
        #endregion
        public TAB50RV()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MESSAGES += "RVABPNEW;";
            MESSAGES += "LOADABP;RESETPROD;";
            InitializeComponent();
            POSTYP = "HM ";
        }
        public override void OnYARNChg()
        {
            if (dwMaster == null)
                return;
            if (YARN.Length < 33)
                return;
            TMV.ABPART = YARN.Substring(0, 20);
            TMV.ABPVAR = YARN.Substring(20, 3);
            TMV.ABPFARBE = YARN.Substring(23, 10);
            base.OnYARNChg();
        }
        public override void RefreshARTCommand(object sender, RoutedEventArgs e)
        {
            PM["dwABP"] = dwMaster;
            PM["ABPAUFKRS"] = TMV.ABPAUFKRS;
            PM["ABPAUFLFD"] = TMV.ABPAUFLFD;
            PM["ABPAUFPOS"] = TMV.ABPAUFPOS;
            PM["ABPART"] = TMV.ABPART;
            PM["ABAVAR"] = TMV.ABPVAR;
            PM["ABAFARBE"] = TMV.ABPFARBE;
            CHGAB50ART(PM);
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            IsCanDelete = false;
            base.vwMaster_CurrentChanged(sender, e);
            if (TMV.ABPZUGKG > 0)
            {
                IsZutei = true;
            }
            else
            {
                IsZutei = false;
            }
            DES = TMV.ABPFARBE;
            ABPSTKLG = TMV.ABPSTKLG;
            ABPSOLLMENGE5 = TMV.ABPSOLLMENGE5;
            ABPSOLLMENGE7 = TMV.ABPSOLLMENGE7;
        }
        #region====WorkFlow========
        MessageInfo Lastmsg
        {
            get
            {
                return TMV.Lastmsg;
            }
            set
            {
                TMV.Lastmsg = value;
            }
        }
        public override void OnWorkflowIDChange(MessageEventArgs e)
        {
            IsBusy = true;
            base.OnWorkflowIDChange(e);
            MessageInfo msg = e.Info;
            string task = msg.MSGTYPE;
            switch (task)
            {
                case "RVABPNEW":
                    if (Lastmsg != null)
                    {
                        MessageBox.Show("请先完成前一个订单创建");
                        //            return;
                    }
                    RULEKEY = "";//先装载规则
                    RULEKEY = msg.ORDER;
                    Lastmsg = msg;
                    ARTST = msg.WAKZ;
                    ART = msg.ART;
                    VAR = msg.VAR;
                    FARBE = msg.FARBE;
                    PRART = ART + VAR + FARBE;
                    PM["ORDER"] = msg.ORDER;
                    PM["WAKZ"] = msg.WAKZ;
                    PM["ART"] = msg.ART;
                    PM["VAR"] = msg.VAR;
                    PM["FARBE"] = msg.FARBE;
                    PM["BESCHICH"] = msg.BESCHICH.PadRight(10);
                    PM["DESS"] = msg.DESS.PadRight(10);
                    PM["COLORIT"] = msg.COLORIT.PadRight(10);
                    PM["GROE"] = msg.COLORIT.PadRight(7);
                    TMV.RVABPNEW(PM);
                    ORDERNUM = msg.ORDER;
                    IsShowAB = false;
                    IsShowAB = true;
                    break;
            }
            IsBusy = false;
        }
        #endregion

        public virtual void btnPrePlan_Click(object sender, RoutedEventArgs e)
        {
            if (dwAB == null)
                return;
            dwData = TMV.VZU.TBMain.NewRow();
            dwData["ZUMANDANT"] = app.Mandant;
            dwData["ZUAUFKRS"] = dwAB["ABPAUFKRS"].ToString();
            dwData["ZUAUFLFD"] = dwAB["ABPAUFLFD"].ToString();
            dwData["ZUAUFPOS"] = dwAB["ABPAUFPOS"].ToString();
            SResult = TMV.VZU.QZU13(dwData);
            if (SResult.Tables[0].Rows.Count > 0)
            {
                MessageBox.Show("请先取消留位，再修改投料比！");
                return;
            }
            if (Lastmsg == null)
            {
                Lastmsg = new MessageInfo();
                Lastmsg.ORDER = dwAB["ABPAUFKRS"].ToString() + dwAB["ABPAUFLFD"].ToString() + dwAB["ABPAUFPOS"].ToString();
                Lastmsg.WAKZ = "G";
                Lastmsg.ART = dwAB["ABPART"].ToString();
                Lastmsg.VAR = dwAB["ABPVAR"].ToString();
                Lastmsg.FARBE = dwAB["ABPFARBE"].ToString();
            }
            PM["ORDER"] = Lastmsg.ORDER;
            PM["WAKZ"] = Lastmsg.WAKZ;
            PM["ART"] = Lastmsg.ART;
            PM["VAR"] = Lastmsg.VAR;
            PM["FARBE"] = Lastmsg.FARBE;
            PM["BESCHICH"] = Lastmsg.BESCHICH.PadRight(10);
            PM["DESS"] = Lastmsg.DESS.PadRight(10);
            PM["COLORIT"] = Lastmsg.COLORIT.PadRight(10);
            PM["GROE"] = Lastmsg.COLORIT.PadRight(7);
            TMV.RVABPNEW(PM);
            if (dwAB != null)
            {
                string key = dwAB["ABPAUFKRS"].ToString() + dwAB["ABPAUFLFD"].ToString() + dwAB["ABPAUFPOS"].ToString() + dwAB["ABPPOSTYP"].ToString();
                ORDERNUM = "";
                ORDERNUM = key;
            }

        }

        private void btnSplitBatch_Click(object sender, RoutedEventArgs e)
        {
            VICC_WFEVENTS.SVWF.AB50SPLIT(RUNID, KEY);
        }

        private void btnRepaire_Click(object sender, RoutedEventArgs e)
        {
            VICC_WFEVENTS.SVWF.AB50RVREP(RUNID, KEY);
        }

        //合投时，修复对应销售单信息。
        private string GETORDERINFO(bool IsDirect)
        {
            string info = "";
            DataSet Set = new DataSet();
            Set.Merge(TBMain);
            Set.Merge(cMUVK.TBMain);
            Set.Merge(cGD.TBMain);
            PM["KRS"] = dwAB["ABPAUFKRS"];
            PM["LFD"] = dwAB["ABPAUFLFD"];
            PM["POS"] = dwAB["ABPAUFPOS"];
            SResult = TMV.VZU.LOADZU(PM);
            Set.Merge(SResult);
            if (IsDirect)
            {
                cDR.PrintType = "ExcelOnly";
            }
            else
            {
                cDR.PrintType = "OpenExcel";
            }
            cDR.PRINT(Set, true);
            return info;
        }
        public override void PrintCommand(object sender, RoutedEventArgs e)
        {
            Button bt = sender as Button;
            string tag = bt.Tag.ToString();
            if (tag == "YGRABFBWC")
            {
                cDR.DocType = "YGRABFBWC";
            }
            else
            {
                cDR.DocType = "YGRABFB";
            }           
            GETORDERINFO(false);
            base.PrintCommand(sender, e);
        }
        public void FBApproveCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwAB == null)
                    return;
                if (dwRule == null)
                    return;
                dwData = TMV.VGD.TBMain.NewRow();
                dwData["GAMANDANT"] = app.Mandant;
                dwData["GAAUFKRS"] = dwAB["ABPAUFKRS"].ToString();
                dwData["GAAUFLFD"] = dwAB["ABPAUFLFD"].ToString();
                dwData["GAAUFPOS"] = dwAB["ABPAUFPOS"].ToString();
                SResult = TMV.VGD.QGD28(dwData);
                if (SResult.Tables[0].Rows.Count > 0)
                {
                    MessageBox.Show("请先完成留位，再确认！");
                    return;
                }
                if (TMV.ABPSPGRD != "89")
                {
                    MessageBox.Show("请先启动复板！");
                    return;
                }
                PM["ORDERNUM"] = dwAB["ABPAUFKRS"].ToString() + dwAB["ABPAUFLFD"].ToString() + dwAB["ABPAUFPOS"].ToString() + dwAB["ABPPOSTYP"].ToString();
                PM["ABPSPGRD"] = "  ";
                TMV.ENDABP(PM);
                TMV.VABF.ENDABF(PM);
                dwData = TMV.VMU.TBMain.NewRow();
                dwData["ABAMANDANT"] = app.Mandant;
                dwData["ABAAUFKRS"] = dwAB["ABPAUFKRS"].ToString();
                dwData["ABAAUFLFD"] = dwAB["ABPAUFLFD"].ToString();
                dwData["ABAAUFPOS"] = dwAB["ABPAUFPOS"].ToString();
                SResult = TMV.VMU.QMU11A(dwData);
                foreach (DataRow dwVKP in SResult.Tables[0].Rows)
                {
                    dwVKP["VKPEXAUFKRS"] = dwAB["ABPAUFKRS"].ToString();
                    dwVKP["VKPEXAUFLFD"] = dwAB["ABPAUFLFD"].ToString();
                    dwVKP["VKPEXAUFPOS"] = dwAB["ABPAUFPOS"].ToString();
                    dwVKP["VKPEXART"] = Helpers.Helper.ConvertGB2312To8859P1("2.复板确认");
                    TMV.VVKP.UVKP11A(dwVKP);
                    PM["CUSER"] = app.User;
                    PM["EVENTID"] = "MYGRABFB07";
                    PM["dwInput"] = dwVKP;
                    PM["KRS"] = dwVKP["VKPAUFKRS"];
                    PM["LFD"] = dwVKP["VKPAUFLFD"];
                    PM["POS"] = dwVKP["VKPAUFPOS"];
                    PM["MSG"] = new MessageInfo();
                    PM["INFO"] = Helpers.Helper.ConvertGB2312To8859P1("复板确认");
                    VICC_WFEVENTS.SVWF.NEWEVENTS(PM);
                    PM["RUNID"] = this.RUNID;
                    PM["VKPAUF"] = dwVKP["VKPAUFKRS"].ToString() + dwVKP["VKPAUFLFD"].ToString() + dwVKP["VKPAUFPOS"].ToString() + "K  ";
                    PM["VKPEXART"] = dwVKP["VKPEXART"].ToString();
                    TMV.VVKP.VKPCHK(PM);
                }
                MessageBox.Show("复板确认完成！");
                if (SResult.Tables[0].Rows.Count > 0)
                {
                    DataRow dwvkp = SResult.Tables[0].Rows[0];
                    if (dwvkp["VKPART"].ToString().Trim().Substring(0, 2) == "45" & dwvkp["VKPEXAUFPOS"].ToString().Trim() == "010")
                    {
                        MessageBox.Show("请调整B纱并确认复板！");
                    }
                }
                cDR.DocType = "YGRABFBWC";
                GETORDERINFO(true);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        public void FBStartCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwAB == null)
                    return;
                if (dwRule == null)
                    return;
                if (TMV.ABPSPGRD == "89")
                {
                    MessageBox.Show("复板已经启动，无需再启动！");
                    return;
                }
                dwData = TMV.VGD.TBMain.NewRow();
                dwData["GAMANDANT"] = app.Mandant;
                dwData["GAAUFKRS"] = dwAB["ABPAUFKRS"].ToString();
                dwData["GAAUFLFD"] = dwAB["ABPAUFLFD"].ToString();
                dwData["GAAUFPOS"] = dwAB["ABPAUFPOS"].ToString();
                SResult = TMV.VGD.QGD28(dwData);
                if (SResult.Tables[0].Rows.Count > 0)
                {
                    MessageBox.Show("请先完成留位，再启动！");
                    return;
                }
                PM["ORDERNUM"] = dwAB["ABPAUFKRS"].ToString() + dwAB["ABPAUFLFD"].ToString() + dwAB["ABPAUFPOS"].ToString() + dwAB["ABPPOSTYP"].ToString();
                //TMV.ENDABP(PM);
                PM["ABPSPGRD"] = "89";
                TMV.REACTIVE(PM);
                cDR.DocType = "YGRABFB";
                GETORDERINFO(true);
                dwData = TMV.VMU.TBMain.NewRow();
                dwData["ABAMANDANT"] = app.Mandant;
                dwData["ABAAUFKRS"] = dwAB["ABPAUFKRS"].ToString();
                dwData["ABAAUFLFD"] = dwAB["ABPAUFLFD"].ToString();
                dwData["ABAAUFPOS"] = dwAB["ABPAUFPOS"].ToString();
                SResult = TMV.VMU.QMU11A(dwData);
                foreach (DataRow dwVKP in SResult.Tables[0].Rows)
                {
                    dwVKP["VKPEXAUFKRS"] = dwAB["ABPAUFKRS"].ToString();
                    dwVKP["VKPEXAUFLFD"] = dwAB["ABPAUFLFD"].ToString();
                    dwVKP["VKPEXAUFPOS"] = dwAB["ABPAUFPOS"].ToString();
                    dwVKP["VKPEXART"] = Helpers.Helper.ConvertGB2312To8859P1("1.启动复板");
                    TMV.VVKP.UVKP11(dwVKP);
                    PM["CUSER"] = app.User;
                    PM["EVENTID"] = "MYGRABFB03";
                    PM["dwInput"] = dwVKP;
                    PM["KRS"] = dwVKP["VKPAUFKRS"];
                    PM["LFD"] = dwVKP["VKPAUFLFD"];
                    PM["POS"] = dwVKP["VKPAUFPOS"];
                    PM["MSG"] = new MessageInfo();
                    PM["INFO"] = Helpers.Helper.ConvertGB2312To8859P1("启动复板");
                    VICC_WFEVENTS.SVWF.NEWEVENTS(PM);
                    PM["RUNID"] = this.RUNID;
                    PM["VKPAUF"] = dwVKP["VKPAUFKRS"].ToString() + dwVKP["VKPAUFLFD"].ToString() + dwVKP["VKPAUFPOS"].ToString() + "K  ";
                    PM["VKPEXART"] = dwVKP["VKPEXART"].ToString();
                    TMV.VVKP.VKPCHK(PM);
                }
                MessageBox.Show("复板启动完成！");
                if (SResult.Tables[0].Rows.Count > 0)
                {
                    DataRow dwvkp = SResult.Tables[0].Rows[0];
                    if (dwvkp["VKPART"].ToString().Trim().Substring(0, 2) == "45" & dwvkp["VKPEXAUFPOS"].ToString().Trim() == "010")
                    {
                        MessageBox.Show("请调整B纱并启动复板！");
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

        private void FBResetCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                PM["ORDERNUM"] = dwAB["ABPAUFKRS"].ToString() + dwAB["ABPAUFLFD"].ToString() + dwAB["ABPAUFPOS"].ToString() + dwAB["ABPPOSTYP"].ToString();
                PM["ABPSPGRD"] = "89";
                TMV.REACTIVE(PM);
                dwData = TMV.VMU.TBMain.NewRow();
                dwData["ABAMANDANT"] = app.Mandant;
                dwData["ABAAUFKRS"] = dwAB["ABPAUFKRS"].ToString();
                dwData["ABAAUFLFD"] = dwAB["ABPAUFLFD"].ToString();
                dwData["ABAAUFPOS"] = dwAB["ABPAUFPOS"].ToString();
                SResult = TMV.VMU.QMU11A(dwData);
                foreach (DataRow dwVKP in SResult.Tables[0].Rows)
                {
                    dwVKP["VKPEXAUFKRS"] = dwAB["ABPAUFKRS"].ToString();
                    dwVKP["VKPEXAUFLFD"] = dwAB["ABPAUFLFD"].ToString();
                    dwVKP["VKPEXAUFPOS"] = dwAB["ABPAUFPOS"].ToString();
                    dwVKP["VKPEXART"] = Helpers.Helper.ConvertGB2312To8859P1("1.启动复板");
                    TMV.VVKP.UVKP11A(dwVKP);
                    PM["CUSER"] = app.User;
                    PM["EVENTID"] = "MYGRABFB03";
                    PM["dwInput"] = dwVKP;
                    PM["KRS"] = dwVKP["VKPAUFKRS"];
                    PM["LFD"] = dwVKP["VKPAUFLFD"];
                    PM["POS"] = dwVKP["VKPAUFPOS"];
                    PM["MSG"] = new MessageInfo();
                    PM["INFO"] = Helpers.Helper.ConvertGB2312To8859P1("重置启动复板");
                    VICC_WFEVENTS.SVWF.NEWEVENTS(PM);
                    PM["RUNID"] = this.RUNID;
                    PM["VKPAUF"] = dwVKP["VKPAUFKRS"].ToString() + dwVKP["VKPAUFLFD"].ToString() + dwVKP["VKPAUFPOS"].ToString() + "K  ";
                    PM["VKPEXART"] = dwVKP["VKPEXART"].ToString();
                    TMV.VVKP.VKPCHK(PM);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }


    }
    public partial class TAB50RV
    {

        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            string CKRS = "", CLFD = "", CPOS = "", CART = "", CFARBE = "";
            double QTYBASE = 0;
            double QTYTOTAL = 0;
            double VKPTOTAL = 0;
            try
            {
                if (!IsCanDelete)
                    throw new System.Exception("必需先勾选删除标志，并且将解散合投关系，并且需要重新进行留位等处理");
                if (dwMUD == null)
                {
                    throw new System.Exception("请选择需要删除的合投单");
                }
                CKRS = dwMUD["ABAPRAUFKRS"].ToString();
                CLFD = dwMUD["ABAPRAUFLFD"].ToString();
                CPOS = dwMUD["ABAPRAUFPOS"].ToString();
                if (CLFD == "000000")
                    throw new Exception("此行为基础单，不可以移除");
                if (dwRule == null)
                {
                    throw new Exception("请选择复板单");
                }
                QTYTOTAL = Convert.ToDouble(dwMaster["ABPSOLLMENGE5"].ToString());
                VKPTOTAL = Convert.ToDouble(dwMaster["ABPSOLLMENGE7"].ToString());
                dwMaster["ABPSOLLMENGE5"] = QTYTOTAL - double.Parse(dwMUD["ABPSOLLMENGE5"].ToString());
                dwMaster["ABPSOLLMENGE7"] = VKPTOTAL - double.Parse(dwMUD["ABPSOLLMENGE7"].ToString());

                TMV.UABP07(dwMaster);
                TMV.VABF.UABF04(dwMaster);
                double QTYMAT = 0;
                double RATIO = Math.Round(Convert.ToDouble(dwMaster["ABPSOLLMENGE5"].ToString()) / QTYTOTAL, 5);

                EventDispatcher.Instance.RaiseStatus("开始解除", StatusEventType.RunningProgress);

                DataSet STemp = new DataSet();
                //找到对应的AB52，进行恢复
                TMV.VMU.UMU09A(dwMUD);

                PM["ORDERNUM"] = RULEKEY;
                SResult = TMV.VGD.LOAD(PM);
                DataTable TBGD = SResult.Tables[0].Copy();
                foreach (DataRow dwGD in TBGD.Rows)
                {
                    dwGD["GAKG1"] = Helper.RoundX(Convert.ToDouble(dwGD["GAKG1"].ToString()) * RATIO, 1);
                    dwGD["GAKG2"] = Helper.RoundX(Convert.ToDouble(dwGD["GAKG2"].ToString()) * RATIO, 1);
                    QTYMAT += Convert.ToDouble(dwGD["GAKG1"].ToString()) * 0.01;
                    TMV.VGD.UGD07(dwGD);
                    TMV.VGL.REDUCEZUTEI(dwGD);
                }

                dwRule["VKPQTY"] = Convert.ToDouble(dwMaster["ABPSOLLMENGE5"].ToString()) * 0.01;
                dwRule["QTYEND"] = Convert.ToDouble(dwMaster["ABPSOLLMENGE7"].ToString()) * 0.01;
                dwRule["QTYMAT"] = QTYMAT;
                TMV.VPR.UpdateICC_PRULES(dwRule, 1);
                dwData = TMV.VVKP.TBMain.NewRow();
                dwData["VKPMANDANT"] = app.Mandant;
                string vkp = dwMUD["ABKKDAUFTNR"].ToString();
                dwData["VKPAUFKRS"] = vkp.Substring(0, 2);
                dwData["VKPAUFLFD"] = "00" + vkp.Substring(3, 4);
                dwData["VKPAUFPOS"] = vkp.Substring(8, 3);
                dwData["VKPEXAUFKRS"] = dwMUD["ABAPRAUFKRS"];
                dwData["VKPEXAUFLFD"] = dwMUD["ABAPRAUFLFD"];
                dwData["VKPEXAUFPOS"] = dwMUD["ABAPRAUFPOS"];
                // dwData["VKPEXART"] = Helper.ConvertGB2312To8859P1("0.合投解除").PadRight(20);
                TMV.VVKP.UVKP11B(dwData);//更新对应的销售单的复板单标识
                dwData = TMV.VMU.TBMain.NewRow();
                dwData["ABAMANDANT"] = app.Mandant;
                dwData["ABAAUFKRS"] = dwMaster["ABPAUFKRS"];
                dwData["ABAAUFLFD"] = dwMaster["ABPAUFLFD"];
                dwData["ABAAUFPOS"] = dwMaster["ABPAUFPOS"];
                SResult = TMV.VMU.QMU05B(dwData);
                if (SResult.Tables[0].Rows.Count == 0)
                {
                    dwData = TMV.VABK.TBMain.NewRow();
                    dwData["ABKMANDANT"] = app.Mandant;
                    dwData["ABKAUFKRS"] = dwMaster["ABPAUFKRS"];
                    dwData["ABKAUFLFD"] = dwMaster["ABPAUFLFD"];
                    dwData["ABKARTSB"] = Helper.ConvertGB2312To8859P1(" ").PadRight(8);
                    TMV.VABK.UABK04(dwData);
                }

                dwData = TMV.VABK.TBMain.NewRow();
                dwData["ABKMANDANT"] = app.Mandant;
                dwData["ABKAUFKRS"] = dwMUD["ABAPRAUFKRS"];
                dwData["ABKAUFLFD"] = dwMUD["ABAPRAUFLFD"];
                dwData["ABKARTSB"] = Helper.ConvertGB2312To8859P1(" ").PadRight(8);
                TMV.VABK.UABK04(dwData);

                PM["ORDERNUM"] = RULEKEY;
                PM["RUNID"] = RUNID;
                TMV.LOADABP(PM);
                IsCanDelete = false;
                base.DeleteCommand(sender, e);

            }
            catch (Exception ex)
            {
                MessageBox.Show("合投有异常发生" + ex.Message);
                Tracer.Error(ex.StackTrace, ex);
                EventDispatcher.Instance.RaiseStatus("异常", StatusEventType.StopProgress);
            }
            finally
            {
                EventDispatcher.Instance.RaiseStatus("合投完成", StatusEventType.StopProgress);
            }
        }

    }
    public partial class VABPOS
    {
        public void RVABPNEW(Dictionary<string, object> pm)
        {
            try
            {
                IsBusy = true;
                if (dwRule["UNDES"].ToString().Trim().Length < 8)
                    return;
                if (PRLO.Length < 2)
                    return;
                if (PRLB.Length < 2)
                    return;
                APZANRKRS = dwRule["UNDES"].ToString().Substring(0, 2);
                APZALFDNR = dwRule["UNDES"].ToString().Substring(2, 6);
                if (APZALFDNR.Trim().Length < 6)
                {
                    throw new Exception("请选择流程");
                }
                //QTYMAT = (QTYEND - F02 + F01) * R01;
                //QTYMAT = VKPQTY * R01;
                double TargetRatio = QTYMAT / QTYEND;//例如1.2，就是100kg成品需要120kg原料
                if (TargetRatio < 0)
                    TargetRatio = 1;

                #region=======dwData==========
                DataRow dwABP = TBMain.NewRow();
                SetUpNewRow(ref dwABP, null);
                ShareedABP(dwABP, pm);
                dwABP["ABPZLO"] = PRLO;
                dwABP["ABPZLB"] = PRLB;
                dwABP["ABPSME"] = "5";
                dwABP["ABPPOSTYP"] = POSTYP;
                dwABP["ABPWAKZ"] = "G";
                dwABP["ABPAUFART"] = "GP";
                dwABP["ABPSOLLMENGE1"] = 0;
                dwABP["ABPSOLLMENGE2"] = 0;
                dwABP["ABPSOLLMENGE3"] = 0;
                dwABP["ABPSOLLMENGE4"] = 0;
                dwABP["ABPSOLLMENGE5"] = QTYEND * 100;
                dwABP["ABPSOLLMENGE6"] = 0;
                dwABP["ABPSOLLMENGE7"] = System.Convert.ToDouble(dwRule["VKPQTY"].ToString()) * 100;//在YGR需要记录销售单数量
                dwABP["ABPSOLLMENGE8"] = 0;
                dwABP["ABPZUGKG"] = 0;
                dwABP["ABPSTKLG"] = Math.Round(TargetRatio * 10000000);
                #endregion
                dwABP["ABPSPGRD"] = 99;
                Insert(dwABP);
                VPR.UpdateICC_PRULES(dwRule, 1);
                PM["KRS"] = KRS;
                //PM["NEWABK"] = msg.P3;
                PM["VKAUF"] = dwRule["P4"];
                //PM["VKAUF"] = msg.KRS + msg.LFD + msg.POS + msg.P1 + msg.AUFTYP;
                PreparePM(dwABP);

                VGD.NEWGD310(PM);
                PM["ABAAUFTYP"] = "V";
                PM["IsMUClear"] = false;
                VMU.MUNEW(PM);

                VPAR.NABPR(PM);

                VABF.ABFNEW(PM);

                PM["ABPSOLLMENGE5"] = dwABP["ABPSOLLMENGE5"].ToString();
                PM["ABPSOLLMENGE7"] = dwABP["ABPSOLLMENGE7"].ToString();
                PM["ABPSTKLG"] = dwABP["ABPSTKLG"].ToString();
                PM["IsRV"] = true;
                VGD.NEWGD713(PM);
                Lastmsg = null;
                PM["RUNID"] = this.RUNID;
                PM["VKPAUF"] = dwRule["P5"];
                PM["VKPEXART"] = "".PadRight(15);
                VVKP.VKPCHK(PM);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
                MessageBox.Show(ex.Message);
            }
            finally
            {
                IsBusy = false;
            }
        }
    }
}
